<?php


namespace Login;


use app\common\lib\RedisKey;
use custom\Curl;
use custom\Log;
use custom\Redis;
use think\Exception;

class WangYiLogin {

    protected static $configs = [
        // 产品密钥
        'secretId'   => '5d500ab49dd4504896ace03af68fb297',
        // 产品私钥
        'secretKey'  => '490377dbe5abfa9eb8c437ec5ba59403',
        // 业务id
        'businessId' => '379f593ff2dc44dc881033df6486c024',
        // 接口版本号
        'version'    => 'v1',
        // 接口域名
        'url'        => 'https://ye.dun.163.com/v1/',
    ];

    /**
     * 一键登录
     * @param $token
     * @param $accessToken
     * @return mixed
     * @throws Exception
     */
    public static function oneClick($token, $accessToken) {
        $url = 'oneclick/check';
        $data = [
            'token'       => (string)$token,
            'accessToken' => (string)$accessToken,
        ];
        $data = array_merge($data, static::initData());
        static::getSign($data);
        $res = static::request($url, $data);
        return $res['data']['phone'];
    }

    /**
     * 本机校验
     * @param $token
     * @param $accessToken
     * @param $phone
     * @throws Exception
     */
    public static function check($token, $accessToken, $phone) {
        $url = 'check';
        $data = [
            'token'       => (string)$token,
            'accessToken' => (string)$accessToken,
            'phone'       => (string)$phone,
        ];
        $data = array_merge($data, static::initData());
        static::getSign($data);
        $res = static::request($url, $data);
    }

    /**
     * 生成签名
     * @param $params
     * @return string
     */
    private static function getSign(&$params) {
        ksort($params);
        $buff = "";
        foreach ($params as $key => $value) {
            $buff .= $key;
            $buff .= $value;
        }
        $buff .= self::$configs['secretKey'];
        $params['signature'] = md5(mb_convert_encoding($buff, "utf8", "auto"));
        return $params['signature'];
    }

    /**
     * 初始化数据
     * @return array
     */
    private static function initData() {
        return [
            'secretId'   => self::$configs['secretId'],
            'businessId' => self::$configs['businessId'],
            'version'    => self::$configs['version'],
            'timestamp'  => get_microtime(),
            'nonce'      => get_nonce_str(32),
        ];
    }

    /**
     * 三方调用
     * @param $url
     * @param array $data
     * @param string $mth
     * @return bool
     * @throws Exception
     */
    public static function request($url, $data = [], $mth = 'POST') {
        $conf = self::$configs;
        $url = $conf['url'] . $url;
        $res = Curl::getInstance()->xhr($mth, $url, http_build_query($data));
        Log::write("$url|" . json_encode($data) . '|res:' . json_encode($res), 'wang_yi_login.log');
        if (empty($res)) {
            throw new Exception('一键调用失败');
        }
        $res = json_decode($res, true);
        if (!isset($res['code']) || $res['code'] != 200) {
            throw new Exception('一键接口调用失败');
        }
        return $res;
    }
}
